package ui.phone

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowPosition
import androidx.compose.ui.window.rememberWindowState
import state.ApplicationWindowsState
import ui.compose.NetworkImage
import ui.compose.RectIconButton


/**
 * @author wei kunJie
 * @date 2024/4/26 13:42:04
 */
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun PhoneWindow(
    applicationWindowsState: ApplicationWindowsState,
    close: () -> Unit
) = Window(
    icon = painterResource("icon/ic_logo.svg"),
    transparent = true,//设置为透明窗口
    undecorated = true, //透明窗口必须取消装饰，这样就没默认的窗口标题栏和那些最小化、最大化、关闭什么的了
    onCloseRequest = close,
    title = "语音通话",
    state = rememberWindowState(width = 330.dp, height = 462.dp, position = WindowPosition(Alignment.Center))
) {
//rememberCoroutineScope() 是一个用于在 Composable 函数中创建协程作用域
    val coroutineScope = rememberCoroutineScope()
    //创建ViewModel
    val viewModel = remember { PhoneViewModel(applicationWindowsState, coroutineScope) }
    val windowsCorner = 5.dp//窗口圆角大小
    //窗口表面，用来定窗口样式
    Surface(
        modifier = Modifier.fillMaxSize().padding(5.dp)
            .shadow(3.dp, RoundedCornerShape(windowsCorner + 3.dp)),//窗口阴影样式
        color = Color(0xFF2B2B2B),
        shape = RoundedCornerShape(windowsCorner) //窗口圆角
    ) {
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Spacer(Modifier.height(100.dp))
            NetworkImage(
                url = viewModel.avatar.value,
                painterFor = { remember { BitmapPainter(it) } },
                contentDescription = "头像",
                modifier = Modifier.size(120.dp).clip(CircleShape),
                contentScale = ContentScale.Crop,
                defaultPainterFor = painterResource("image/default_head.png")
            )
            Spacer(Modifier.weight(1f))
            Text(text = viewModel.phoneState.value, color = Color.White)
            Spacer(Modifier.height(18.dp))
            Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(bottom = 30.dp)) {
                RectIconButton(
                    onClick = {
                        viewModel.closeOrOpenMicrophone()
                    },
                    modifier = Modifier.size(54.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(color = Color(0xFFF5FAFF)),
                ) {
                    Icon(
                        painter = if (viewModel.isMicrophone.value) painterResource("icon/ic_no_microphone.svg")
                        else painterResource("icon/ic_microphone.svg"),
                        contentDescription = "麦克风",
                        tint = Color(0xFF2B2B2B),
                        modifier = Modifier.size(40.dp).padding(10.dp)
                    )
                }
                Spacer(Modifier.width(30.dp))
                RectIconButton(
                    onClick = {
                        viewModel.closeOrOpenVoice()
                    },
                    modifier = Modifier.size(54.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(color = Color(0xFFF5FAFF)),
                ) {
                    Icon(
                        painter = if (viewModel.isVoice.value) painterResource("icon/ic_no_phone_voice.svg")
                        else painterResource("icon/ic_phone_voice.svg"),
                        contentDescription = "听筒",
                        tint = Color(0xFF2B2B2B),
                        modifier = Modifier.size(40.dp).padding(10.dp)
                    )
                }
                Spacer(Modifier.width(30.dp))
                RectIconButton(
                    onClick = {
                        viewModel.closePhone()
                    },
                    modifier = Modifier.size(54.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(color = Color.Red),
                ) {
                    Icon(
                        painter = painterResource("icon/ic_phone2.svg"),
                        contentDescription = "挂断",
                        tint = Color.White,
                        modifier = Modifier.size(40.dp).padding(10.dp)
                    )
                }
            }
        }
    }
    //窗口可拖动区域，鼠标按住后可以拖动窗口
    WindowDraggableArea {
        Surface(
            modifier = Modifier.fillMaxWidth().height(Dp(40f)).padding(5.dp),
            color = Color.Transparent,
            shape = RoundedCornerShape(windowsCorner, windowsCorner)
        ) {
            val isMouseIn = remember { mutableStateOf(false) }
            Box(
                contentAlignment = Alignment.TopEnd, modifier = Modifier
            ) {
                RectIconButton(
                    onClick = close,
                    modifier = Modifier.size(33.dp).background(
                        color = if (isMouseIn.value) Color(0xFFC42B1C)
                        else Color.Transparent
                    ).onPointerEvent(PointerEventType.Enter) { isMouseIn.value = true }
                        .onPointerEvent(PointerEventType.Exit) { isMouseIn.value = false },
                ) {
                    Icon(
                        painter = painterResource("icon/ic_close.svg"),
                        contentDescription = "关闭",
                        tint = Color.White,
                        modifier = Modifier.size(33.dp).padding(9.dp)
                    )
                }
            }
        }
    }
}